/*
 * @Author: your name
 * @Date: 2021-08-08 21:50:23
 * @LastEditTime: 2021-11-20 13:38:03
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /blogServe/src/controller/moment.controller.js
 */
const momentService = require('../service/moment.service');

class MomentController {
  async create(ctx, next) {
    // 1.获取数据
    const userId = ctx.user.id;
    const content = ctx.request.body.content;
    const label = ctx.request.body.label;
    const classification = ctx.request.body.classification;
    const instructions = ctx.request.body.instructions;
    // 2. 将数据插入到数据库中
    const result = await momentService.create(content, userId, label, classification, instructions);
    ctx.body = result;
  }

  // 根据Id来查询某一条动态数据
  async detail(ctx, next) {
    // 1.获取数据（momentId）
    const momentId = ctx.params.momentId;
    // 2.根据id去查询某一条动态数据
    const result = await momentService.getMomentById(momentId);
    ctx.body = result;
  }

  // 分页查询列表数据
  async list(ctx, next) {
    // 获取数据(offset/size)
    const { offset, size } = ctx.query;
    // 2.查询列表
    const result = await momentService.getMomentList(offset, size);
    ctx.body = result;
  }

  async update(ctx, next) {
    // 1.获取参数
    const { momentId } = ctx.params;
    const { content } = ctx.request.body;
    const result = await momentService.update(content, momentId);
    ctx.body = {
      code: 200,
      result
    };
  }

  async remove(ctx, next) {
    const { momentId } = ctx.params;
    // 删除内容
    const result = await momentService.remove(momentId);
    ctx.body = {
      code: 200,
      msg: "删除成功",
      result
    }
  }
}

module.exports = new MomentController();